#include <57header.h>

int main(int argc,char*argv[])
{
    char *port = "8080";
    char *ip = "192.168.106.129";


    int net_fd = socket(AF_INET, SOCK_STREAM, 0);

    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(atoi(port));
    addr.sin_addr.s_addr = inet_addr(ip);
    connect(net_fd, (struct sockaddr *)&addr, sizeof(addr));

    // 先接收文件整体大小
    off_t file_size = 0;
    recv(net_fd, &file_size, sizeof(off_t), MSG_WAITALL);


    int name_len = 0;
    recv(net_fd, &name_len, sizeof(int), MSG_WAITALL);

    char fileName[60] = {0};
    recv(net_fd, fileName, name_len, MSG_WAITALL);

    int file_fd = open(fileName, O_RDWR|O_CREAT, 0600);

    // mmap
    // 修改文件大小
    ftruncate(file_fd, file_size);

    void *p = mmap(NULL, file_size,  PROT_READ|PROT_WRITE, MAP_SHARED, file_fd, 0);

    recv(net_fd, p, file_size, MSG_WAITALL);

    munmap(p, file_size);


    close(file_fd);
    close(net_fd);
    return 0;
}

